// ==UserScript==
// @name         百度搜索结果过滤
// @namespace    https://gitee.com/janbar/tampermonkey
// @version      0.0.1
// @description  去掉不想展示的结果
// @author       janbar
// @match        https://www.baidu.com/s*
// @icon         https://www.baidu.com/favicon.ico
// @license      MIT License
// @grant        window.onurlchange
// @grant        GM_xmlhttpRequest
// @connect      baidu.com
// ==/UserScript==

(function () {
    'use strict';

    function filter(node) {
        var url = node.getElementsByTagName('a')[0].href, txt = node.innerText;
        if (txt.indexOf('CSDN') > -1) {
            if (txt.indexOf("csdn已为您找到关于") > -1 || txt.indexOf("CSDN下载") > -1) {
                node.style.display = "none";
                return;
            }
            GM_xmlhttpRequest({
                method: "GET", url: url, timeout: 8000,
                onload: function (result) {
                    if (result.finalUrl.indexOf('csdn.net/tags') > -1 ||
                        result.finalUrl.indexOf('download.csdn.net') > -1) {
                        node.style.display = "none";
                    }
                }
            });
        }
    }

    function filterUrl() {
        var lp = setInterval(function () {
            var cnt = 0, downList = document.getElementsByClassName("result c-container new-pmd");
            for (var i = 0; i < downList.length; i++) {
                if (downList[i].getAttribute('filter_url') === null) {
                    downList[i].setAttribute("filter_url", "0"); // 不会重复执行
                    filter(downList[i]);
                    cnt++;
                }
            }
            if (cnt == 0) {
                clearInterval(lp);
            }
        }, 1000);
    }

    filterUrl();
    if (window.onurlchange === null) {
        window.addEventListener('urlchange', filterUrl)
    }
})();
